package main

import (
	"fmt"
	"runtime"
	"sync"
	"time"
)

var (
	count int64 = 0
	wg    sync.WaitGroup
	mx    sync.Mutex
	//	ch    = make(chan int64, 1)
	ch = make(chan int64, 0)
)

func inc() {
	defer wg.Done()
	for i := 0; i < 100000; i++ {
		//count++
		//atomic.AddInt64(&count, 1)
		// mx.Lock()
		// count++
		// mx.Unlock()
		t := <-ch
		t++
		//fmt.Println(t, "###")
		ch <- t

	}
}

func main() {
	wg.Add(5)

	start := time.Now()
	for i := 0; i < 5; i++ {

		go inc()
	}
	ch <- 0
	fmt.Printf("協程數 %d\n", runtime.NumGoroutine())
	wg.Wait()
	fmt.Printf("執行時長為%d\n", time.Since(start).Microseconds())
	fmt.Printf("協程數 %d\n", runtime.NumGoroutine())
	//fmt.Println(count)
	fmt.Printf("count=%d\n", <-ch)
	fmt.Printf()
}
